package com.biz.primus.management.adv.controller;

import com.biz.primus.base.vo.PageVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.adv.enums.AdType;
import com.biz.primus.model.adv.enums.AdUsePlace;
import com.biz.primus.model.adv.enums.UseType;
import com.biz.primus.model.adv.vo.AdPositionVo;
import com.biz.primus.model.adv.vo.AdvertisementQueryVo;
import com.biz.primus.model.adv.vo.AdvertisementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.biz.primus.management.adv.feign.AdvFeignClient;
import com.biz.primus.management.base.controller.AbstractBaseController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * @author jun.liu
 * @date 2017/9/19
 */
@Controller
@RequestMapping("advertisement/")
public class AdvController extends AbstractBaseController {

    private final AdvFeignClient advFeignClient;

    @Autowired
    public AdvController(@Qualifier(value = "advFeignClient") AdvFeignClient advFeignClient) {
        this.advFeignClient = advFeignClient;
    }

    /**
     * 分页查询所有广告
     *
     * @return
     */
    @GetMapping("ad/list")
    public ModelAndView adList(AdvertisementQueryVo vo) {
        if (vo == null) {
            vo = new AdvertisementQueryVo();
        }
        SpringPageVO<AdvertisementVo> advertisementVoPage = advFeignClient.getAdsByParams(vo);
        return new ModelAndView("advertisement/ad/list", "advertisementVoPage", advertisementVoPage);
    }

    /**
     * 跳转到添加广告信息页面
     */
    @GetMapping("ad/new")
    public ModelAndView newPage() {
        ModelAndView view = new ModelAndView("advertisement/ad/new");
        view.addObject("adTypeEnums", AdType.values());
        List<AdPositionVo> adPositionVos = advFeignClient.getAdPositions();
//        List<AreaResponseVo> areaResponseVos= geoService.findAllProvince();
        view.addObject("adPositionVos", adPositionVos);
//        view.addObject("areaResponseVos",areaResponseVos);
        return view;
    }

    /**
     * 保存广告
     *
     * @param advo
     * @param result
     * @return
     */
    @PostMapping("ad/save")
    public ModelAndView save(@Valid AdvertisementVo advo, BindingResult result) {
        error(result);
        advFeignClient.addOrUpdateAdInfo(advo);
        return adList(new AdvertisementQueryVo());
    }

    /**
     * 根据id删除广告
     *
     * @param adId
     * @return
     */
    @PostMapping("ad/deleteByAdId")
    @ResponseBody
    public String deleteByAdId(@Valid Long adId) {
        advFeignClient.deleteAdById(adId);
        return "true";
    }

    /**
     * 修改 进入new页面
     *
     * @param id
     * @return
     */
    @RequestMapping("ad/{id}")
    public ModelAndView editByAdId(@PathVariable Long id) {
        ModelAndView view = new ModelAndView("advertisement/ad/new");
        view.addObject("adTypeEnums", AdType.values());
        List<AdPositionVo> adPositionVos = advFeignClient.getAdPositions();
//        List<AreaResponseVo> areaResponseVos= geoService.findAllProvince();
        view.addObject("adPositionVos", adPositionVos);
        view.addObject("advertisement",advFeignClient.getAdById(id));
        return view;
    }

    /**
     * 分页查询所有广告位
     *
     * @return
     */
    @GetMapping("adv/list")
    public ModelAndView advList(PageVo vo) {
        if (vo == null) {
            vo = new PageVo();
        }
        SpringPageVO<AdPositionVo> adPositionVoPage = advFeignClient.getAllAdPositions(vo);
        return new ModelAndView("advertisement/adv/list", "adPositionVoPage", adPositionVoPage);
    }

    /**
     * 跳转到添加广告位信息页面
     */
    @GetMapping("adv/new")
    public ModelAndView advNewPage() {
        ModelAndView view = new ModelAndView("advertisement/adv/new");
        view.addObject("adUsePlaceEnums", AdUsePlace.values());
        view.addObject("useTypeEnums", UseType.values());
        return view;
    }

    /**
     * 保存广告位
     *
     * @param advVo
     * @param result
     * @return
     */
    @PostMapping("adv/save")
    public ModelAndView save(@Valid AdPositionVo advVo, BindingResult result) {
        error(result);
        advFeignClient.addOrUpdateAdPositionInfo(advVo);
        return advList(new PageVo());
    }

    /**
     * 根据id删除广告位
     *
     * @param advId
     * @return
     */
    @PostMapping("adv/deleteByAdvId")
    @ResponseBody
    public String deleteByAdvId(@Valid Long advId) {
        advFeignClient.deleteAdPositionById(advId);
        return "true";
    }

    /**
     * 修改 进入new页面
     *
     * @param id
     * @return
     */
    @RequestMapping("adv/{id}")
    public ModelAndView editByAdvId(@PathVariable Long id) {
        ModelAndView view = new ModelAndView("advertisement/adv/new");
        view.addObject("adUsePlaceEnums", AdUsePlace.values());
        view.addObject("useTypeEnums", UseType.values());
        view.addObject("adPosition", advFeignClient.getAdPositionById(id));
        return view;
    }

}
